import { defineStore } from "pinia";
import { ref } from 'vue'



export const uesGroupStore = defineStore('nav-group',()=>{
	const group = ref([
		{
			id:'1b6b11ee-43f5-4afd-8e29-08f91fe26ac3',
			title:'常用',
			items:[
				{
					id:'8de423ec-7f16-4f13-b988-ef049beaa2cb',
					url:'http://www.baidu.com',
					title:'百度'
				},
				{
					id:'bde13ead-fc92-4aee-ba18-79dacbde0827',
					url:'http://www.bilibili.com',
					title:'哔哩哔哩'
				},
				{
					id:'0f989feb-b09e-47e3-a7e1-1811d62fa1ba',
					url:'http://www.douyin.com',
					title:'抖音'
				},
				{
					id:'4df0be79-a5b3-4293-9914-54ae70e8aaeb',
					url:'https://www.xiaohongshu.com/',
					title:'小红书'
				}
			]
		}
	])

	const changeGroupTitle = (id:string,newTitle:string)=>{
		for(let i = 0;i<group.value.length;i++)
		{
			if(id === group.value[i].id)
			{
				group.value[i].title = newTitle
				return
			}
		}
	}
	const removeGroup = (id:string)=>{
		group.value = group.value.filter(item => item.id != id)
	}
	const changeItemUrl = (parentId:string,itemId:string,newUrl:string)=>{
		for(let i = 0;i<group.value.length;i++)
		{
			if(parentId === group.value[i].id)
			{
				for(let j = 0;j<group.value[i].items.length;j++)
				{
					if(itemId === group.value[i].items[j].id)
					{
						group.value[i].items[j].url = newUrl
						return
					}
				}
			}
		}
	}
	const changeItemTitle = (parentId:string,itemId:string,newTitle:string)=>{
		for(let i = 0;i<group.value.length;i++)
		{
			if(parentId === group.value[i].id)
			{
				for(let j = 0;j<group.value[i].items.length;j++)
				{
					if(itemId === group.value[i].items[j].id)
					{
						group.value[i].items[j].title = newTitle
						return
					}
				}
			}
		}
	}
	const removeItem = (parentId:string,itemId:string)=>{
		for(let i = 0;i<group.value.length;i++)
		{
			if(parentId === group.value[i].id)
			{
				group.value[i].items = group.value[i].items.filter(item => item.id != itemId)
			}
		}
	}
	const addItem =(parentId:string,obj:{id:string,url:string,title:string})=>{
		for(let i = 0;i<group.value.length;i++)
		{
			if(parentId === group.value[i].id)
			{
				group.value[i].items.push(obj)
			}
		}
	}
	const addGroup = (obj:any)=>{
		group.value.push(obj)
	}
	return {
		group,
		changeGroupTitle,
		changeItemUrl,
		changeItemTitle,
		removeGroup,
		removeItem,
		addItem,
		addGroup
	}
},{
	persist:true
})